package org.example.diet_compass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.diet_compass.domain.Goods;
import org.example.diet_compass.dto.ChildCategoryItem;
import org.example.diet_compass.dto.TopCategoryItem;
import org.example.diet_compass.service.GoodsService;
import org.example.diet_compass.mapper.GoodsMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 86135
 * @description 针对表【goods(商品项)】的数据库操作Service实现
 * @createDate 2025-02-27 15:21:30
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Double getGoodPriceById(String goodId) {
        return goodsMapper.getGoodPriceById(goodId);
    }

    @Override
    public void insertGoodsBatch(List<Map<String,Object>> mapList) {
        goodsMapper.insertGoodsBatch(mapList);
    }

    @Override
    public List<TopCategoryItem> getGoodsByCategory() {
        //查询数据库
        List<Map<String, Object>> categoryGoods = goodsMapper.getCategoryGoods();

        //组装数据

        Map<String, TopCategoryItem> topCategoryItemMap = new LinkedHashMap<>();

        for (Map<String, Object> row : categoryGoods) {
            String topCategoryName = (String) row.get("topCategoryName");
            String topCategoryId = (String) row.get("topCategoryId");
            String childCategoryName = (String) row.get("childCategoryName");
            String childCategoryId = (String) row.get("childCategoryId");
            //一级分类不存在（首次分类时 创建
            TopCategoryItem topCategoryItem = topCategoryItemMap.computeIfAbsent(topCategoryName, k -> {
                TopCategoryItem topCategoryItemDTO = new TopCategoryItem();
                topCategoryItemDTO.setName(topCategoryName);
                topCategoryItemDTO.setId(topCategoryId);
                topCategoryItemDTO.setChildren(new ArrayList<>());
                return topCategoryItemDTO;
            });
            //查找二级分类
            ChildCategoryItem childCategoryItem = topCategoryItem.getChildren().stream()
                    .filter(child -> child.getName().equals(childCategoryName))
                    .findFirst().orElseGet(() -> {
                        ChildCategoryItem childCategoryItemDTO = new ChildCategoryItem();
                        childCategoryItemDTO.setName(childCategoryName);
                        childCategoryItemDTO.setId(childCategoryId);
                        childCategoryItemDTO.setGoodsList(new ArrayList<>());
                        topCategoryItem.getChildren().add(childCategoryItemDTO);
                        return childCategoryItemDTO;
                    });
            //将map转为Goods对象
            Goods goods = new Goods();
            goods.setId((String) row.get("id"));
            goods.setName((String) row.get("name"));
            goods.setDescription((String) row.get("description"));
            goods.setPrice((Double) row.get("price"));
            goods.setDiscount((BigDecimal) row.get("discount"));
            goods.setPicture((String) row.get("picture"));
            goods.setOrderNum((Integer) row.get("order_num"));
            goods.setAverageScore((BigDecimal) row.get("average_score"));
            childCategoryItem.getGoodsList().add(goods);

        }

        return new ArrayList<>(topCategoryItemMap.values());
    }

    @Override
    public List<Goods> getRecommendsOrderByAVGScore() {
        return goodsMapper.getRecommendsOrderByAVGScore();
    }

    @Override
    public IPage<Goods> getGoodsByPageAndKeyword(int currentPage, int pageSize) {
        IPage<Goods> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        return goodsMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Goods getGoodById(String id) {
        return goodsMapper.getGoodById(id);
    }

    @Override
    public void updateScore( String goodsId) {
        goodsMapper.updateAVGScore(goodsId);
    }


}




